use metadata_extractor::DefaultMetadataExtractor;
use model_graph_types::{
    connector::{Connector, QueryOptions},
    database::{ConnectorProtocol, DataSource, DatabaseType},
    extractor::{ExtractFromDatabaseOption, MetadataExtractor, MetadataPackage},
    modeling::{Class, ModelingReader, ModelingWriter, Value},
};
use union_connector::UnionConnector;

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    let extractor = DefaultMetadataExtractor {};

    let sqlite_data_source = DataSource {
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/suhs/rrg01/rrg01.sqlite"),
        username: None,
        password: None,
        options: None,
    };

    let mysql_data_source = DataSource {
        database_type: DatabaseType::MySQL,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("mysql://root:6e4a1a8993be1628@39.106.1.82:8306/rrai_prod"),
        username: None,
        password: None,
        options: None,
    };

    let dist_data_source = DataSource {
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/suhs/rrg01/rrg01.sqlite"),
        username: None,
        password: None,
        options: None,
    };

    let jdbc_data_source = DataSource {
        database_type: DatabaseType::MySQL,
        protocol: ConnectorProtocol::Jdbc,
        url: String::from(
            "jdbc:mysql://root:6e4a1a8993be1628@39.106.1.82:8306/rrai_prod?useSSL=false",
        ),
        username: Some(String::from("root")),
        password: Some(String::from("6e4a1a8993be1628")),
        options: None,
    };

    let dgraph_data_source = DataSource {
        database_type: DatabaseType::Dgraph,
        protocol: ConnectorProtocol::Dgraph,
        url: String::from("http://192.168.95.190:49080"),
        username: None,
        password: None,
        options: None,
    };

    // let connector = UnionConnector::open(&jdbc_data_source).await.unwrap();

    // let sql = format!("select c.ordinal_position, c.column_name,( CASE  WHEN c.column_default is NULL  THEN ''  ELSE c.column_default  END  ) AS column_default, c.is_nullable, c.data_type,c.column_type,c.column_key,c.extra  from information_schema.COLUMNS c WHERE c.TABLE_SCHEMA = database() and c.table_name = ?  order by c.ordinal_position");

    // let res = connector
    //     .query(
    //         &sql,
    //         &vec![Value::String(String::from("prompts_categories"))],
    //         QueryOptions {},
    //     )
    //     .await;
    let connector = UnionConnector::open(&dgraph_data_source).await.unwrap();

    let sql = String::from(
        r#"
    query{
        data(func: has(type.identifier)){
            uid
            name: type.identifier,
            type.property{
                uid
                name: property.name
            }
        }
    }
    "#,
    );

    let res = connector
        .query(
            &sql,
            &vec![Value::String(String::from("prompts_categories"))],
            QueryOptions {},
        )
        .await;

    match res {
        Ok(result) => {
            tracing::debug!("!!!!!!{:?}", result);
        }
        Err(err) => tracing::debug!("===-----{:?}", err),
    }
}
